<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>中间件 | Python Web 框架要点</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C05-Database/index.html" />
    
    
    <link rel="prev" href="../C04-ClassViewAndMiddleware/ClassView.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="5.2"
        data-chapter-title="中间件"
        data-filepath="C04-ClassViewAndMiddleware/Middleware.md"
        data-basepath=".."
        data-revision="Sat May 05 2018 07:17:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python Web 框架要点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutDjango.html">
            
                
                    <a href="../AboutDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/StartProject.html">
            
                
                    <a href="../C01-CreateProject/StartProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        创建工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/StartApp.html">
            
                
                    <a href="../C01-CreateProject/StartApp.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        创建子应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/CreateView.html">
            
                
                    <a href="../C01-CreateProject/CreateView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        创建视图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-SettingsStaticAndURL/index.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        配置、静态文件与路由
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-SettingsStaticAndURL/Settings.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/Settings.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-SettingsStaticAndURL/StaticFiles.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/StaticFiles.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C02-SettingsStaticAndURL/URL.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/URL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        路由说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C02-SettingsStaticAndURL/AppConfig.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/AppConfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        App应用配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/Request.html">
            
                
                    <a href="../C03-RequestAndResponse/Request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        请求Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/Response.html">
            
                
                    <a href="../C03-RequestAndResponse/Response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        响应Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/Cookie.html">
            
                
                    <a href="../C03-RequestAndResponse/Cookie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C03-RequestAndResponse/Session.html">
            
                
                    <a href="../C03-RequestAndResponse/Session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-ClassViewAndMiddleware/index.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        类视图与中间件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-ClassViewAndMiddleware/ClassView.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/ClassView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        类视图
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5.2" data-path="C04-ClassViewAndMiddleware/Middleware.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Database/index.html">
            
                
                    <a href="../C05-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Database/Config.html">
            
                
                    <a href="../C05-Database/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C05-Database/Model.html">
            
                
                    <a href="../C05-Database/Model.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        定义模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C05-Database/Tools.html">
            
                
                    <a href="../C05-Database/Tools.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        演示工具使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C05-Database/CURD.html">
            
                
                    <a href="../C05-Database/CURD.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="C05-Database/QuerySet.html">
            
                
                    <a href="../C05-Database/QuerySet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        查询集QuerySet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="C05-Database/Manager.html">
            
                
                    <a href="../C05-Database/Manager.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        管理器Manager
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C06-Template/index.html">
            
                
                    <a href="../C06-Template/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C06-Template/template.html">
            
                
                    <a href="../C06-Template/template.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        模板使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C07-Form/index.html">
            
                
                    <a href="../C07-Form/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        表单
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C07-Form/form.html">
            
                
                    <a href="../C07-Form/form.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        表单使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C08-Admin/index.html">
            
                
                    <a href="../C08-Admin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        Admin站点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C08-Admin/Config.html">
            
                
                    <a href="../C08-Admin/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        使用Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="C08-Admin/ModifyListDisplay.html">
            
                
                    <a href="../C08-Admin/ModifyListDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        调整列表页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="C08-Admin/ModifyEditDisplay.html">
            
                
                    <a href="../C08-Admin/ModifyEditDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        调整编辑页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="C08-Admin/ModifySite.html">
            
                
                    <a href="../C08-Admin/ModifySite.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        调整站点信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="C08-Admin/UploadImage.html">
            
                
                    <a href="../C08-Admin/UploadImage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Python Web 框架要点</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x4E2D;&#x95F4;&#x4EF6;">&#x4E2D;&#x95F4;&#x4EF6;</h1>
<p>Django&#x4E2D;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x662F;&#x4E00;&#x4E2A;&#x8F7B;&#x91CF;&#x7EA7;&#x3001;&#x5E95;&#x5C42;&#x7684;&#x63D2;&#x4EF6;&#x7CFB;&#x7EDF;&#xFF0C;&#x53EF;&#x4EE5;&#x4ECB;&#x5165;Django&#x7684;&#x8BF7;&#x6C42;&#x548C;&#x54CD;&#x5E94;&#x5904;&#x7406;&#x8FC7;&#x7A0B;&#xFF0C;&#x4FEE;&#x6539;Django&#x7684;&#x8F93;&#x5165;&#x6216;&#x8F93;&#x51FA;&#x3002;&#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x8BBE;&#x8BA1;&#x4E3A;&#x5F00;&#x53D1;&#x8005;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x65E0;&#x4FB5;&#x5165;&#x5F0F;&#x7684;&#x5F00;&#x53D1;&#x65B9;&#x5F0F;&#xFF0C;&#x589E;&#x5F3A;&#x4E86;Django&#x6846;&#x67B6;&#x7684;&#x5065;&#x58EE;&#x6027;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x5728;Django&#x5904;&#x7406;&#x89C6;&#x56FE;&#x7684;&#x4E0D;&#x540C;&#x9636;&#x6BB5;&#x5BF9;&#x8F93;&#x5165;&#x6216;&#x8F93;&#x51FA;&#x8FDB;&#x884C;&#x5E72;&#x9884;&#x3002;</p>
<h2 id="1-&#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;">1 &#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;</h2>
<p>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x5DE5;&#x5382;&#x51FD;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x522B;&#x8C03;&#x7528;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x3002;</p>
<p>&#x4E2D;&#x95F4;&#x4EF6;&#x5DE5;&#x5382;&#x51FD;&#x6570;&#x9700;&#x8981;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x7684;get_response&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x8FD4;&#x56DE;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x5E76;&#x4E14;&#x50CF;&#x89C6;&#x56FE;&#x4E00;&#x6837;&#x9700;&#x8981;&#x63A5;&#x6536;&#x4E00;&#x4E2A;request&#x5BF9;&#x8C61;&#x53C2;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;response&#x5BF9;&#x8C61;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">simple_middleware</span><span class="hljs-params">(get_response)</span>:</span>
    <span class="hljs-comment"># &#x6B64;&#x5904;&#x7F16;&#x5199;&#x7684;&#x4EE3;&#x7801;&#x4EC5;&#x5728;Django&#x7B2C;&#x4E00;&#x6B21;&#x914D;&#x7F6E;&#x548C;&#x521D;&#x59CB;&#x5316;&#x7684;&#x65F6;&#x5019;&#x6267;&#x884C;&#x4E00;&#x6B21;&#x3002;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">middleware</span><span class="hljs-params">(request)</span>:</span>
        <span class="hljs-comment"># &#x6B64;&#x5904;&#x7F16;&#x5199;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x5728;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x89C6;&#x56FE;&#x524D;&#x88AB;&#x8C03;&#x7528;&#x3002;</span>

        response = get_response(request)

        <span class="hljs-comment"># &#x6B64;&#x5904;&#x7F16;&#x5199;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x5728;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x89C6;&#x56FE;&#x4E4B;&#x540E;&#x88AB;&#x8C03;&#x7528;&#x3002;</span>

        <span class="hljs-keyword">return</span> response

    <span class="hljs-keyword">return</span> middleware
</code></pre>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x5728;users&#x5E94;&#x7528;&#x4E2D;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;middleware.py&#x6587;&#x4EF6;&#xFF0C;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_middleware</span><span class="hljs-params">(get_response)</span>:</span>
    print(<span class="hljs-string">&apos;init &#x88AB;&#x8C03;&#x7528;&apos;</span>)
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">middleware</span><span class="hljs-params">(request)</span>:</span>
        print(<span class="hljs-string">&apos;before request &#x88AB;&#x8C03;&#x7528;&apos;</span>)
        response = get_response(request)
        print(<span class="hljs-string">&apos;after response &#x88AB;&#x8C03;&#x7528;&apos;</span>)
        <span class="hljs-keyword">return</span> response
    <span class="hljs-keyword">return</span> middleware
</code></pre>
<p><strong>&#x5B9A;&#x4E49;&#x597D;&#x4E2D;&#x95F4;&#x4EF6;&#x540E;&#xFF0C;&#x9700;&#x8981;&#x5728;settings.py &#x6587;&#x4EF6;&#x4E2D;&#x6DFB;&#x52A0;&#x6CE8;&#x518C;&#x4E2D;&#x95F4;&#x4EF6;</strong></p>
<pre><code class="lang-python">MIDDLEWARE = [
    <span class="hljs-string">&apos;django.middleware.security.SecurityMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.sessions.middleware.SessionMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.middleware.common.CommonMiddleware&apos;</span>,
    <span class="hljs-comment"># &apos;django.middleware.csrf.CsrfViewMiddleware&apos;,</span>
    <span class="hljs-string">&apos;django.contrib.auth.middleware.AuthenticationMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.messages.middleware.MessageMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.middleware.clickjacking.XFrameOptionsMiddleware&apos;</span>,
    <span class="hljs-string">&apos;users.middleware.my_middleware&apos;</span>,  <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x4E2D;&#x95F4;&#x4EF6;</span>
]
</code></pre>
<p>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x89C6;&#x56FE;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">demo_view</span><span class="hljs-params">(request)</span>:</span>
    print(<span class="hljs-string">&apos;view &#x89C6;&#x56FE;&#x88AB;&#x8C03;&#x7528;&apos;</span>)
    <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;OK&apos;</span>)
</code></pre>
<p>&#x6267;&#x884C;&#x7ED3;&#x679C;</p>
<p><img src="../images/middleware_demo.png" alt="&#x4E2D;&#x95F4;&#x4EF6;&#x6D4B;&#x8BD5;"></p>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;Django&#x8FD0;&#x884C;&#x5728;&#x8C03;&#x8BD5;&#x6A21;&#x5F0F;&#x4E0B;&#xFF0C;&#x4E2D;&#x95F4;&#x4EF6;init&#x90E8;&#x5206;&#x6709;&#x53EF;&#x80FD;&#x88AB;&#x8C03;&#x7528;&#x4E24;&#x6B21;&#x3002;</strong></p>
<h2 id="2-&#x591A;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;">2 &#x591A;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;</h2>
<ul>
<li>&#x5728;&#x8BF7;&#x6C42;&#x89C6;&#x56FE;&#x88AB;&#x5904;&#x7406;<strong>&#x524D;</strong>&#xFF0C;&#x4E2D;&#x95F4;&#x4EF6;<strong>&#x7531;&#x4E0A;&#x81F3;&#x4E0B;</strong>&#x4F9D;&#x6B21;&#x6267;&#x884C;</li>
<li>&#x5728;&#x8BF7;&#x6C42;&#x89C6;&#x56FE;&#x88AB;&#x5904;&#x7406;<strong>&#x540E;</strong>&#xFF0C;&#x4E2D;&#x95F4;&#x4EF6;<strong>&#x7531;&#x4E0B;&#x81F3;&#x4E0A;</strong>&#x4F9D;&#x6B21;&#x6267;&#x884C;</li>
</ul>
<p><img src="../images/middleware_sequence.png" alt="&#x4E2D;&#x95F4;&#x4EF6;&#x987A;&#x5E8F;"></p>
<p>&#x793A;&#x4F8B;&#xFF1A;</p>
<p>&#x5B9A;&#x4E49;&#x4E24;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_middleware</span><span class="hljs-params">(get_response)</span>:</span>
    print(<span class="hljs-string">&apos;init &#x88AB;&#x8C03;&#x7528;&apos;</span>)
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">middleware</span><span class="hljs-params">(request)</span>:</span>
        print(<span class="hljs-string">&apos;before request &#x88AB;&#x8C03;&#x7528;&apos;</span>)
        response = get_response(request)
        print(<span class="hljs-string">&apos;after response &#x88AB;&#x8C03;&#x7528;&apos;</span>)
        <span class="hljs-keyword">return</span> response
    <span class="hljs-keyword">return</span> middleware

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_middleware2</span><span class="hljs-params">(get_response)</span>:</span>
    print(<span class="hljs-string">&apos;init2 &#x88AB;&#x8C03;&#x7528;&apos;</span>)
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">middleware</span><span class="hljs-params">(request)</span>:</span>
        print(<span class="hljs-string">&apos;before request 2 &#x88AB;&#x8C03;&#x7528;&apos;</span>)
        response = get_response(request)
        print(<span class="hljs-string">&apos;after response 2 &#x88AB;&#x8C03;&#x7528;&apos;</span>)
        <span class="hljs-keyword">return</span> response
    <span class="hljs-keyword">return</span> middleware
</code></pre>
<p>&#x6CE8;&#x518C;&#x6DFB;&#x52A0;&#x4E24;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;</p>
<pre><code class="lang-python">MIDDLEWARE = [
    <span class="hljs-string">&apos;django.middleware.security.SecurityMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.sessions.middleware.SessionMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.middleware.common.CommonMiddleware&apos;</span>,
    <span class="hljs-comment"># &apos;django.middleware.csrf.CsrfViewMiddleware&apos;,</span>
    <span class="hljs-string">&apos;django.contrib.auth.middleware.AuthenticationMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.messages.middleware.MessageMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.middleware.clickjacking.XFrameOptionsMiddleware&apos;</span>,
    <span class="hljs-string">&apos;users.middleware.my_middleware&apos;</span>,  <span class="hljs-comment"># &#x6DFB;&#x52A0;</span>
    <span class="hljs-string">&apos;users.middleware.my_middleware2&apos;</span>,  <span class="hljs-comment"># &#x6DFB;&#x52A0;</span>
]
</code></pre>
<p>&#x6267;&#x884C;&#x7ED3;&#x679C;</p>
<pre><code class="lang-python">init2 &#x88AB;&#x8C03;&#x7528;
init &#x88AB;&#x8C03;&#x7528;
before request &#x88AB;&#x8C03;&#x7528;
before request <span class="hljs-number">2</span> &#x88AB;&#x8C03;&#x7528;
view &#x89C6;&#x56FE;&#x88AB;&#x8C03;&#x7528;
after response <span class="hljs-number">2</span> &#x88AB;&#x8C03;&#x7528;
after response &#x88AB;&#x8C03;&#x7528;
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C04-ClassViewAndMiddleware/ClassView.html" class="navigation navigation-prev " aria-label="Previous page: 类视图"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C05-Database/index.html" class="navigation navigation-next " aria-label="Next page: 数据库"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
